package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.Document;

import java.io.IOException;
import java.io.InputStream;

/**
 * 获取pdf文本内容工具类
 */
public class PDFUtil {

    public static String pdfToString(InputStream is) {

        PDDocument doc = null;
        try {
            //加载pdf文档
             doc = PDDocument.load(is);
            //创建提取文本，转换器
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            String text = pdfTextStripper.getText(doc);
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if (is != null){
                    is.close();
                }
                if (doc != null) {
                    doc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
